﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;
using ZHDJ.WebPage;

namespace JoinParty.admin.FlowStep
{
    public partial class RegistrationStepFive : PageBase
    {
        public string Content { get; set; }

        public string RequestContent { get; set; }
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            this.ppBranch.Value = CurrentUser.BranchID;
            this.ppBranch.Text = ControlBusiness.GetBranchPickerText(CurrentUser.BranchID);
            this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {
            partyjoinMeetingFive meet = DB.partyjoinMeetingFives.Find(this.CurrentStandID);
            if (meet != null)
            {
                this.ppBranch.Value = meet.BranchID;
                this.ppBranch.Text = ControlBusiness.GetBranchPickerText(meet.BranchID);
                this.txtMeetingTitle.Text = meet.MeetingTitle;
                this.txtMeetingAdress.Text = meet.MeetingAdress;
                this.Content = meet.MeetingContent;
                this.dtTime.Text = meet.MeetingTime.ToString("yyyy-MM-dd");
                this.ppMeetingHost.Value = meet.MeetingHost;
                this.ppMeetingHost.Text = ControlBusiness.GetMembersPickerText(meet.MeetingHost);
                this.ppMeetingNotes.Value = meet.MeetingNotes;
                this.ppMeetingNotes.Text = ControlBusiness.GetMembersPickerText(meet.MeetingNotes);
                this.ppJoinMember.Value = meet.MeetingAttends;
                this.ppJoinMember.Text = ControlBusiness.GetMembersPickerText(meet.MeetingAttends);
                this.RequestContent = meet.MeetingRequest;
            }
            else
            {
                this.Content = string.Format("会议内容：<div><div><div>1.____年__月__日，本支部派______通知与_____谈话，了解基本情况</div><div>2.了解的基本情况为______________</div><div>3.据了解，该同志基本具备入党条件。</div></div></div>");
                this.RequestContent = string.Format("兹有我支部_____，性别____，____年___月出生。<br>该同志在培养考察期间，能够积极向党组织靠拢，符合入党积极分子各项条件。<br>____年__月__日经我党支部大会推荐，一致同意该同志为入党积极分子。<br>妥否？请批复！");
            }

            if (this.ReadOnly == "1")
            {
                this.btnSaveClose.Hidden = true;
                this.btnSubmit.Hidden = true;
            }
        }

        private void SaveData(bool isClose, bool IsSubmit = false)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过
            bool bResult = true;
            PartyJoinLinkExample oModel = null;
            oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            var meet = DB.partyjoinMeetingFives.Find(this.CurrentStandID);
            if (meet == null)
            {
                meet = new partyjoinMeetingFive();
            }
            meet.BranchID = this.ppBranch.Value;
            meet.MeetingTitle = this.txtMeetingTitle.Text;
            meet.MeetingAdress = this.txtMeetingAdress.Text;
            meet.MeetingTime = Convert.ToDateTime(this.dtTime.Text);
            meet.MeetingHost = this.ppMeetingHost.Value;
            meet.MeetingNotes = this.ppMeetingNotes.Value;
            meet.MeetingAttends = this.ppJoinMember.Value;
            meet.MeetingContent = this.Request.Form["editor"];
            meet.MeetingRequest = this.Request.Form["editorRequest"]; ;

            if (string.IsNullOrEmpty(meet.StandID))
            {
                meet.StandID = this.CurrentStandID;
                DB.partyjoinMeetingFives.Add(meet);
                DB.SaveChanges();
            }
            else
                DB.SaveChanges();
            if (IsSubmit)
            {
                oModel.HandleUserID = this.CurrentUser.ID;
                oModel.HandleUserName = this.CurrentUser.UserName;
                oModel.HandleTime = DateTime.Now;
                oModel.Status = (int)Enum_FlowStanceStatus.通过;
                bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);
            }


        }
        #region 按钮
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckValue())
            {
                this.SaveData(true, true);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            if (CheckValue())
            {
                SaveData(true);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }
        #endregion

        public bool CheckValue()
        {
            bool ret = true;
            if (this.txtMeetingAdress.Text.Trim() == "")
            {
                this.txtMeetingAdress.MarkInvalid("请填写会议地点");
                ret = false;
            }
            if (this.dtTime.Text.Trim() == "")
            {
                this.dtTime.MarkInvalid("请选择会议时间");
                ret = false;
            }
            if (this.ppMeetingHost.Text.Trim() == "")
            {
                this.ppMeetingHost.MarkInvalid("必填");
                ret = false;
            }
            if (this.ppMeetingNotes.Text.Trim() == "")
            {
                this.ppMeetingNotes.MarkInvalid("必填");
                ret = false;
            }
            if (this.txtMeetingTitle.Text.Trim() == "")
            {
                this.txtMeetingTitle.MarkInvalid("请填写会议主题");
                ret = false;
            }
            if (this.ppJoinMember.Text.Trim() == "")
            {
                this.ppJoinMember.MarkInvalid("必填");
                ret = false;
            }

            return ret;
        }
    }
}